import requests
import unittest
from api.login import LoginAPI
from parameterized import parameterized
import json
# from cns import MySQL
# 刘世琪的技术博客

def build_data():
    temp = []
    # conn = MySQL.DBHelper('127.0.0.1', 3306, 'root', '123456', 'test')
    with open(".//data//data.json", "r", encoding="utf-8") as f:
        json_f = json.load(f)

        for item in json_f:
            print(item)
            print(type(item))
            d = (item["status_code"], item["content_type"], item["data"], item["status"], item["msg"])

            temp.append(d)
            print(temp)
        return temp
    # data = {
    #     "username": "18501218888",
    #     "password": "123456",
    #     "verify_code": "8888",
    # }
    # return [(200, "image", data, 1, "登陆成功")]


class TestLogin(unittest.TestCase):

    def setUp(self):
        # 在每次执行 测试用例之前  做初始化操作
        self.api = LoginAPI()
        self.session = requests.session()

    def tearDown(self):
        # 释放资源
        self.session.close()

    @parameterized.expand(build_data)
    def test01_success(self, status_code, content_type, data, status, msg):
        # 调用 LoginAPI类 提供的 访问 图片验证码的方法
        resp = self.api.do_verify_code(self.session)

        self.assertEqual(status_code, resp.status_code)
        self.assertIn(content_type, resp.headers.get("Content-Type"))

        # data = {
        #     "username": "18501218888",
        #     "password": "jiyun123456",
        #     "verify_code": "8888",
        # }
        # 调用 LoginAPI提供的 登陆方法
        resp = self.api.do_login(self.session, data)

        self.assertEqual(status_code, resp.status_code)
        # self.assertEqual(status, resp.status)
        self.assertEqual(status, resp.json().get("status"))
        self.assertEqual(msg, resp.json().get("msg"))
        # print(a,b,c)
